require 'rubygems'
require 'yaml'
require 'daemons'
require 'weather_bot'
require 'logger'

class WeatherDaemon
  def initialize
    config = YAML.load(File.read("config"))
    @logger = Logger.new('twit-weather.log')
    
    @weather_bots = []
    config.each do |bot|
      bot << @logger
      @weather_bots << WeatherBot.new(*bot)
    end
  end

  def start
    @threads = []
    @weather_bots.each do |bot|
      @threads << Thread.new do
        loop do
          bot.run
          sleep 5
        end
      end
    end

    @threads.each { |thread| thread.join }
  end
end

options = {
  :backtrace => true,
  :dir_mode => :system,
  :log_output => true
}

daemon = WeatherDaemon.new
Daemons.run_proc('WeatherDaemon', options) { daemon.start }
